(function(angular) {
	"use strict";
	angular.module("fs.waf").controller("apf.limitRequest.viewController", viewController);
	viewController.$inject = [ "$scope", "$q", "modalService", "layoutConfig", "validatorConfig", "authService", "limitRequestService" ];
	function viewController($scope, $q, modalService, layoutConfig, validatorConfig, authService, limitRequestService) {
		var ctrl = this, params = $scope.$stateParams.params;
		ctrl.validatorConfig = validatorConfig;

		ctrl.init = function() {
			authService.view(params).then(function(response) {
				if (response.data) {
					ctrl.layoutConfig = layoutConfig;
					ctrl.limitRequest = response.data;
				}
			});
		};

		ctrl.view = function() {
			modalService.openCustomDialogs({
				"title" : "title.popup.responsemessages",
				"dialogsModule" : "apf.limitRequest.viewResponse",
				"params" : ctrl.limitRequest,
				"btnCallback" : {
					"ok" : function(resultList) {
					}
				}
			});
		};

		ctrl.submit = function() {

			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ "title.dialogs.info.norecsel" ]
				});
				return;
			}
			return authService.process(ctrl.selectedRows[0]).then(submitSuccessful, submitFailed);
		};

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				"messages" : [ "title.dialog.successful.submit" ],
				"btnCallback" : {
					"close" : function() {
						$scope.go("apf.workflow.wfTxn.list");
					}
				},
				"redirections" : [ {
					"name" : "title.infobar.workflow.list",
					"state" : "apf.workflow.wfTxn.list",
					"params" : {}
				}, {
					"name" : "menu.extrequest.list",
					"state" : "apf.limitRequest.list",
					"params" : {}
				} ]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				"messages" : [ "error appeared: " + angular.toJson(response.error) ]
			});
		}
	}
})(angular);
